// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages;

import java.util.List;

import lichen.entities.forum.Forum;
import lichen.entities.forum.ForumImpl;
import lichen.pages.base.BasePage;
import lichen.pages.base.LogInfoPage;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.corelib.components.Loop;
import org.apache.tapestry.corelib.components.PageLink;
import org.hibernate.Query;

/**
 * 论坛的首页面
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 7 $
 * @since 0.0.1
 */
public class Index extends BasePage implements LogInfoPage{
	
	//查询一级论坛,1 为论坛的大类，2 为顶级论坛.
	private static final String QUERY_FORUM_HQL = "from "
			+ ForumImpl.class.getName() + " f where " +
					"f."+ForumImpl.DEPTH_PRO_NAME+" <3 " +
							"and f."+ForumImpl.LEFT_PRO_NAME+">? " +
									"and f."+ForumImpl.RIGHT_PRO_NAME+"<?";


	private static final String QUERY_CATEGORY_HQL = "from "
			+ ForumImpl.class.getName() + " f where f."+ForumImpl.DEPTH_PRO_NAME+" =1";

	
	// 循环中使用的变量
	private Forum forum;
	
	//循环中适用的变量
	private Forum category;

//	循环展示论坛信息
	@SuppressWarnings("unused")
	@Component(parameters = { "source=forums", "value=forum" })
	private Loop forumsLoop;
//	循环展示论坛信息
	@SuppressWarnings("unused")
	@Component(parameters = { "source=categories", "value=category" })
	private Loop categoriesLoop;
	//跳转到论坛的组件
	@SuppressWarnings("unused")
	@Component(parameters = {"page=forum/view","context=forum"})
	private PageLink viewForumLink;

	/**
	 * 查询所有的论坛.
	 * 
	 * @return 论坛列表.
	 */
	public List getForums() {
		Query query=this.getHibernateSession().createQuery(QUERY_FORUM_HQL);
		query.setParameter(0,this.category.getLeft());
		query.setParameter(1,this.category.getRight());
		return query.list();
	}
	/**
	 * @return Returns the forum.
	 */
	public Forum getForum() {
		return forum;
	}

	/**
	 * @param forum The forum to set.
	 */
	public void setForum(Forum forum) {
		this.forum = forum;
	}
	
	public String getForumCssClassName(){
		if(this.forum.getDepth()==1){
			return "class-header";
		}else{
			return "forum";
		}
	}
	public boolean isCategoryForum(){
		return this.forum.getDepth() == 1;
	}
	public List getCategories(){
		return this.getHibernateSession().createQuery(QUERY_CATEGORY_HQL).list();
	}
	/**
	 * @return Returns the category.
	 */
	public Forum getCategory() {
		return category;
	}
	/**
	 * @param category The category to set.
	 */
	public void setCategory(Forum category) {
		this.category = category;
	}
	public String getLogInfo() {
		return this.getMessages().get("view-home-page");
	}
}
